package com.jswl.training.springjpa.netty.client;

import com.jswl.training.springjpa.util.HexByteUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class NettyClientHandler extends ChannelInboundHandlerAdapter  {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("{} :客户端开始登录!",new Date());

        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("{} :客户端开始断开连接!",new Date());
        super.channelInactive(ctx);

        String hexMsg = "7B 7B 7B 00 00 00 00 A2 30 01 62 00 60 09 0B 00 94 02 00 00 11 D3 11 2C 01 11 40 42 0F 00 00 00 00 " +
                "00 0B 00 00 00 0B 00 0B 00 0B 00 0C 00 D0 02 00 00 22 D4 11 2C 01 11 40 42 0F 00 00 00 00 00 0C 00 00 00 0C 00 0C 00 0C " +
                "00 0D 00 0C 03 00 00 33 D5 11 2C 01 11 40 42 0F 00 00 00 00 00 0D 00 00 00 0D 00 0D 00 0D 00 0E 00 48 03 00 00 44 D6 11 " +
                "2C 01 11 40 42 0F 00 00 00 00 00 0E 00 00 00 0E 00 0E 00 0E 00 0F 00 84 03 00 00 55 D7 11 2C 01 11 40 42 0F 00 00 00 00 " +
                "00 0F 00 00 00 0F 00 0F 00 0F 00 10 00 C0 03 00 00 11 D8 11 2C 01 11 40 42 0F 00 00 00 00 00 10 00 00 00 10 00 10 00 10 " +
                "00 11 00 FC 03 00 00 11 D9 11 2C 01 11 40 42 0F 00 00 00 00 00 11 00 00 00 11 00 11 00 11 00 12 00 38 04 00 00 33 DA 11 " +
                "2C 01 11 40 42 0F 00 00 00 00 00 12 00 00 00 12 00 12 00 12 00 13 00 74 04 00 00 44 DB 11 2C 01 11 40 42 0F 00 00 00 00 " +
                "00 13 00 00 00 13 00 13 00 13 00 14 00 B0 04 00 00 55 DC 11 2C 01 11 40 42 0F 00 00 00 00 00 14 00 00 00 14 00 14 00 14 " +
                "00 F8 00 7B 7B 7B 00 00 00 00 A3 7E 00 61 00 D0 03 01 11 00 7E CA 85 F5 FF FF FF 00 82 35 7A 0A 00 00 00 00 7E CA " +
                "85 F5 FF FF FF 00 FA E2 0F 96 FF FF FF 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 " +
                "7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 " +
                "7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 01 00" ;
        byte[] msgBytes = HexByteUtil.hexStrToByteArrs(hexMsg);
        ctx.channel().writeAndFlush(Unpooled.buffer().writeBytes(msgBytes));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        super.channelRead(ctx, msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
